// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import {utils} from "../utils/utils";
import {globalConfig} from "../config/globalConfig";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Confirm extends cc.Component {

  @property(cc.Node)
  private btnConfirm: cc.Node = null;

  @property(cc.Node)
  private btnCancel: cc.Node = null;

  @property(cc.Label)
  private labelDuck: cc.Label = null;

  @property(cc.Sprite)
  private iconSprite: cc.Sprite = null;

  @property(cc.SpriteFrame)
  private coinSpriteFrame: cc.SpriteFrame = null;

  @property(cc.SpriteFrame)
  private diamondSpriteFrame: cc.SpriteFrame = null

  @property(cc.Node)
  private buyNode: cc.Node = null;

  @property(cc.Label)
  private labelTips: cc.Label = null;

  // LIFE-CYCLE CALLBACKS:

  onLoad() {
    utils.btnify(this.btnConfirm, () => {
        this.node.active = false;
        this.onConfirm();
      }
    )
    utils.btnify(this.btnCancel, () => {
      this.node.active = false;
      if (this.onCancel) {
        this.onCancel();
      }
    })
  }

  showMerge(text: string, confirm: Function, cancel?: Function) {
    this.node.active = true;
    this.labelTips.node.active = true;
    this.buyNode.active = false;
    this.labelTips.string = text;
    this.onConfirm = confirm;
    this.onCancel = cancel;
  }

  showBuy(id: number, confirm: Function, cancel ?: Function) {
    this.node.active = true;

    this.labelTips.node.active = false;
    this.buyNode.active = true;
    const item: any = globalConfig.item.find((value: any) => value.id === id)
    this.labelDuck.string = item.cost
    if (item.currencytype === 1) {
      this.iconSprite.spriteFrame = this.diamondSpriteFrame;
    } else if (item.currencytype === 2) {
      this.iconSprite.spriteFrame = this.coinSpriteFrame;
    }
    this.onConfirm = confirm;
    this.onCancel = cancel;
  }

  onConfirm() {

  }

  onCancel() {

  }
}
